package com.mytrip.webapp.action;

import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONObject;

import com.mytrip.base.action.StrutsActionAdapter;
import com.mytrip.model.AddressBooks;
import com.mytrip.model.AreaBizTongcheng;
import com.mytrip.model.AreaTongcheng;
import com.mytrip.model.CityChainTongcheng;
import com.mytrip.model.FlightCity;
import com.mytrip.model.OrderFlight;
import com.mytrip.model.OrderHotel;
import com.mytrip.service.OrderManager;
import com.mytrip.service.UserManager;
import com.mytrip.util.PageInfo;
import com.mytrip.vo.HotelInfo;
import com.mytrip.webapp.filter.CtxSessionBag;

public class OrderAction extends StrutsActionAdapter{

	private static final long serialVersionUID = 5793245629883892447L;

	private OrderManager orderManager;

	private UserManager userManager;

	private PageInfo<OrderFlight> orderFlightPage = new PageInfo<OrderFlight>();

	private PageInfo<OrderHotel> orderHotelPage = new PageInfo<OrderHotel>();

	private PageInfo<HotelInfo> hotelInfoPage = new PageInfo<HotelInfo>(5);

	public String flightSearchPre() throws Exception{
		return "1".equals(getRequest().getParameter("t")) ? "flightSearchPre_2" : "flightSearchPre";
	}

	public String hotelSearchPre() throws Exception{
		return "1".equals(getRequest().getParameter("t")) ? "hotelSearchPre_2" : "hotelSearchPre";
	}

	public String flightSearch() throws Exception{
		String fromCity = getRequest().getParameter("fromCity");
		String toCity = getRequest().getParameter("toCity");
		String fromDate = getRequest().getParameter("fromDate");
		String isTj = getRequest().getParameter("isTj");
		String viaPort = getRequest().getParameter("viaPort");

		String flyType = getRequest().getParameter("flyType");
		if ("2".equals(flyType)) {
			getRequest().setAttribute("backDate", getRequest().getParameter("backDate"));
		}

		FlightCity _fromCity = orderManager.queryFlightCityByCity3code(fromCity);
		FlightCity _toCity = orderManager.queryFlightCityByCity3code(toCity);

		String jsonRes = orderManager.queryFlightByApi(fromCity, toCity, fromDate);
		getRequest().setAttribute("jsonRes", jsonRes);

		List<AddressBooks> addressBooksList = userManager.findAddressBooksByUser();

		getRequest().setAttribute("addressBooksList", addressBooksList);
		getRequest().setAttribute("fromCity", _fromCity);
		getRequest().setAttribute("toCity", _toCity);
		getRequest().setAttribute("fromDate", fromDate);
		getRequest().setAttribute("isTj", isTj);
		getRequest().setAttribute("viaPort", viaPort);
		return "1".equals(getRequest().getParameter("t")) ? "flightSearch_2" : "flightSearch";
	}

	public String hotelSearch() throws Exception{
		String toCity = getRequest().getParameter("toCity");
		String fromDate = getRequest().getParameter("fromDate");
		String toDate = getRequest().getParameter("toDate");
		String key = getRequest().getParameter("key");
		key = key != null ? (key.trim().replaceAll("'|&|\\?", "")) : "";

		String priceRange = getRequest().getParameter("priceRange"); // 最低,最高
		String area = getRequest().getParameter("area");
		String areaBiz = getRequest().getParameter("areaBiz");
		String chain = getRequest().getParameter("chain");

		StringBuilder sb = new StringBuilder(128);
		sb.append("toCity=" + toCity);
		sb.append("&fromDate=" + fromDate);
		sb.append("&toDate=" + toDate);
		sb.append("&key=" + key);

		if (priceRange != null && !"".equals(priceRange)) {
			sb.append("&priceRange=" + priceRange);
		}
		if (area != null && !"".equals(area)) {
			sb.append("&area=" + area);
		}
		if (areaBiz != null && !"".equals(areaBiz)) {
			sb.append("&areaBiz=" + areaBiz);
		}
		if (chain != null && !"".equals(chain)) {
			sb.append("&chain=" + chain);
		}

		hotelInfoPage.setParamName("hotelInfoPage");
		String t = "1".equals(getRequest().getParameter("t")) ? "t=1&" : "";
		hotelInfoPage.setUrl(getRequest().getContextPath() + "/front/order!hotelSearch.action?" + t + sb.toString());

		orderManager.queryHotelByApi(toCity, fromDate, toDate, key, priceRange, area, areaBiz, chain, hotelInfoPage);

		getRequest().setAttribute("hotelInfoPage", hotelInfoPage);

		Integer cityId = Integer.parseInt(toCity);
		getRequest().setAttribute("toCity", orderManager.queryHotelCityByTongchengId(cityId));
		getRequest().setAttribute("fromDate", fromDate);
		getRequest().setAttribute("toDate", toDate);
		getRequest().setAttribute("key", key);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		getRequest().setAttribute("duringDate",
				(sdf.parse(toDate).getTime() - sdf.parse(fromDate).getTime()) / (1000 * 60 * 60 * 24));

		List<AreaTongcheng> areaList = orderManager.queryHotelAreaByCity(cityId);
		List<AreaBizTongcheng> areaBizList = orderManager.queryHotelAreaBizByCity(cityId);
		List<CityChainTongcheng> chainList = orderManager.queryHotelChainByCity(cityId);

		getRequest().setAttribute("areaList", areaList);
		getRequest().setAttribute("areaBizList", areaBizList);
		getRequest().setAttribute("chainList", chainList);

		getRequest().setAttribute("priceRange", priceRange);
		getRequest().setAttribute("area", area);
		getRequest().setAttribute("areaBiz", areaBiz);
		getRequest().setAttribute("chain", chain);

		return "1".equals(getRequest().getParameter("t")) ? "hotelSearch_2" : "hotelSearch";
	}

	public String confirmPAT(String fromCity, String toCity, String flightNo, String departureDate, String carrier,
			String code, String price, String isTj, String tjId, String checkKey) throws Exception{
		return orderManager.queryFlightPATByApi(fromCity, toCity, flightNo, departureDate, carrier, code, price, isTj,
				tjId, checkKey);
	}

	public String confirmOrder(String fromCity, String toCity, String flightNo, String departureDate,
			String departureTime, String arrivalDate, String arrivalTime, String carrier, String code, String price,
			String airportTax, String fuelSurTax, String isTj, String tjId, String checkKey, String peopleStr,
			Integer addressBooksId) throws Exception{
		return orderManager.confirmOrder(fromCity, toCity, flightNo, departureDate, departureTime, arrivalDate,
				arrivalTime, carrier, code, price, airportTax, fuelSurTax, isTj, tjId, checkKey, peopleStr,
				addressBooksId);
	}

	public String confirmOrderHotel(String hotelId, String roomTypeId, String hotelType, String bookingCode,
			String fromDate, String toDate, String roomNum, String guestNum, String name_lx, String tel_lx,
			String name_rz, String tel_rz, String come_time, String hotelName, String roomName, String avgAmount,
			String hotelAddress){
		return orderManager.confirmHotelOrder(hotelId, roomTypeId, hotelType, bookingCode, fromDate, toDate, roomNum,
				guestNum, name_lx, tel_lx, name_rz, tel_rz, come_time, hotelName, roomName, avgAmount, hotelAddress);
	}

	public String saveAddressBook(String address, String zipcode, String consignee, String phone){
		AddressBooks addressBooks = new AddressBooks();
		addressBooks.setAddress(address);
		addressBooks.setConsignee(consignee);
		addressBooks.setPhone(phone);
		addressBooks.setZipcode(zipcode);
		Integer id = userManager.saveAddressBooks(addressBooks);
		return id + "";
	}

	public String queryAddressBookById(Integer adid) throws Exception{
		AddressBooks res = userManager.findAddressBooksById(adid);
		if (!res.getUserid().equals(CtxSessionBag.getSessionBag().getUserinfo().getId())) {
			throw new Exception("permission error");
		}
		JSONObject obj = new JSONObject();
		obj.put("address", res.getAddress());
		obj.put("zipcode", res.getZipcode());
		obj.put("consignee", res.getConsignee());
		obj.put("phone", res.getPhone());

		return obj.toString();
	}

	public String queryRoomsByHotelId(Integer hotelId, String fromDate, String toDate){
		return orderManager.queryRoomsByHotelIdByApi(hotelId, fromDate, toDate);
	}

	public String showFlight() throws Exception{
		orderFlightPage.setParamName("orderFlightPage");
		orderFlightPage.setUrl(getRequest().getContextPath() + "/front/order!showFlight.action");
		orderFlightPage.setAllSize(orderManager.queryOrderFlightCnt());
		orderFlightPage.setDataList(orderManager.queryOrderFlight(orderFlightPage.getFrom(), orderFlightPage
				.getPageSize()));
		return "1".equals(getRequest().getParameter("t")) ? "showFlight_2" : "showFlight";
	}

	public String showHotel() throws Exception{
		orderHotelPage.setParamName("orderHotelPage");
		orderHotelPage.setUrl(getRequest().getContextPath() + "/front/order!showHotel.action");
		orderHotelPage.setAllSize(orderManager.queryOrderHotelCnt());
		orderHotelPage
				.setDataList(orderManager.queryOrderHotel(orderHotelPage.getFrom(), orderHotelPage.getPageSize()));
		return "1".equals(getRequest().getParameter("t")) ? "showHotel_2" : "showHotel";
	}

	public void setOrderManager(OrderManager orderManager){
		this.orderManager = orderManager;
	}

	public void setUserManager(UserManager userManager){
		this.userManager = userManager;
	}

	public PageInfo<OrderFlight> getOrderFlightPage(){
		return orderFlightPage;
	}

	public void setOrderFlightPage(PageInfo<OrderFlight> orderFlightPage){
		this.orderFlightPage = orderFlightPage;
	}

	public PageInfo<HotelInfo> getHotelInfoPage(){
		return hotelInfoPage;
	}

	public void setHotelInfoPage(PageInfo<HotelInfo> hotelInfoPage){
		this.hotelInfoPage = hotelInfoPage;
	}

	public PageInfo<OrderHotel> getOrderHotelPage(){
		return orderHotelPage;
	}

	public void setOrderHotelPage(PageInfo<OrderHotel> orderHotelPage){
		this.orderHotelPage = orderHotelPage;
	}

}
